﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Automobile.service.Model;

namespace Automobile.service.DAL
{
    public class EmployeeDal
    {
        string str = "Data Source=.;Initial Catalog=qichefuwu;Integrated Security=True";
        /// <summary>
        /// 用户显示
        /// </summary>
        /// <returns></returns>
        public List<EmployeeModel> xian()
        {
            string sql = "select * from Employee";
            SqlDataAdapter apter = new SqlDataAdapter(sql, str);
            DataTable table = new DataTable();
            apter.Fill(table);
            List<EmployeeModel> list = new List<EmployeeModel>();
            foreach (DataRow item in table.Rows)
            {
                EmployeeModel e = new EmployeeModel();
                e.Employee_Id = Convert.ToInt32(item["Employee_Id"]);
                e.Employee_Name = item["Employee_Name"].ToString();
                e.Employee_Pwd = item["Employee_Pwd"].ToString();
                e.Employee_Phone = item["Employee_Phone"].ToString();
                e.Employee_Addr = item["Employee_Addr"].ToString();
                e.Employee_State = item["Employee_State"].ToString();
                e.Employee_Remark = item["Employee_Remark"].ToString();
                list.Add(e);
            }
            return list;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="uname"></param>
        /// <param name="upwd"></param>
        /// <returns></returns>
        public int login(string Employee_Name, string Employee_Pwd)
        {
            //查询sql语句
            string sql = "select Employee_Id,Employee_Name,Employee_Pwd  from Employee where Employee_Name='" + Employee_Name + "' and Employee_Pwd='" + Employee_Pwd + "'";
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand com = new SqlCommand(sql, conn);
            int i = Convert.ToInt32(com.ExecuteScalar());
            conn.Close();
            return i;
        }
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public int AddEmployee(EmployeeModel e)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string st = string.Format("insert into Employee values('{0}','{1}','{2}','{3}','{4}','{5}')", e.Employee_Name, e.Employee_Pwd, e.Employee_Phone, e.Employee_Addr, e.Employee_State, e.Employee_Remark);
            SqlCommand cmd = new SqlCommand(st, conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
            return i;
        }
        /// <summary>
        /// 取单个用户信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetEmployee(string Employee_Id)
        {
            string sql = "select Employee_Id,Employee_Name,Employee_Pwd,Employee_Phone,Employee_Addr,Employee_State,Employee_Remark from Employee where Employee_Id=" + Employee_Id;

            SqlConnection conn = new SqlConnection(str);
            conn.Open();//打开数据库

            SqlCommand comm = new SqlCommand(sql, conn);
            DataTable table = new DataTable();
            SqlDataAdapter ada = new SqlDataAdapter(comm);
            ada.Fill(table);
            conn.Close();
            return table;
        }
        /// <summary>
        ///修改用户信息
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        public int Update(EmployeeModel em)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string st = string.Format("update Employee set Employee_Name={0},Employee_Pwd='{1}',Employee_Phone='{2}',Employee_Addr='{3}',Employee_State='{4}',Employee_Remark='{5}' where Employee_Id={6}",em.Employee_Name, em.Employee_Pwd, em.Employee_Phone, em.Employee_Addr, em.Employee_State, em.Employee_Remark, em.Employee_Id);
            SqlCommand cmd = new SqlCommand(st, conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();          
            return i;
           
        }
        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelEmployee(string id)
        {
            //连接对象
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string sql = "delete from Employee where Employee_Id in (" + id + ")";
            SqlCommand cmd = new SqlCommand(sql, conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();
            return i;
        }
    }
}
